﻿#pragma once
#include "Lumina2D.h"
#include <iostream>
const int SQUARE_SIZE = 30;

class Square
{
public:
	Square(int x, int y)
	{
		this->x = x;
		this->y = y;
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
				has_square[i][j] = 0;
	}

	void Draw()
	{
		int real_x = (int)((x - 0) / SQUARE_SIZE) * SQUARE_SIZE + 0;
		int real_y = (int)((y - 100) / SQUARE_SIZE) * SQUARE_SIZE + 100;
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (has_square[i][j])
				{
					ShapeRenderer::DrawRect(real_x + j * SQUARE_SIZE, real_y + i * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE, true, LIGHTGRAY, WHITE);
				}
			}
		}
	}

	void Move(float dx, float dy)
	{
		x += dx;
		y += dy;
	}

	bool CheckCollisionLeft(int begin_x, int begin_y, int left_wall, bool out_has_square[15][10])
	{
		int real_x = (int)((x - begin_x) / SQUARE_SIZE) * SQUARE_SIZE + begin_x;
		int real_y = (int)((y - begin_y) / SQUARE_SIZE) * SQUARE_SIZE + begin_y;

		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (has_square[i][j])
				{
					int temp_x = real_x + j * SQUARE_SIZE;
					int temp_y = real_y + i * SQUARE_SIZE;
					int botton_line_y = temp_y + SQUARE_SIZE;

					if (temp_x < left_wall)
					{
						return 1;
					}

					int index_x = (temp_y - begin_y) / SQUARE_SIZE;
					int index_y = (temp_x - begin_x) / SQUARE_SIZE;

					if (index_x >= 0 && index_x < 15 && index_y >= 0 && index_y < 10)
					{
						if (out_has_square[index_x][index_y])
						{
							return 1;
						}
					}
				}
			}
		}
		return 0;
	}
	bool CheckCollisionRight(int begin_x, int begin_y, int right_wall, bool out_has_square[15][10])
	{
		int real_x = (int)((x - begin_x) / SQUARE_SIZE) * SQUARE_SIZE + begin_x;
		int real_y = (int)((y - begin_y) / SQUARE_SIZE) * SQUARE_SIZE + begin_y;

		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (has_square[i][j])
				{
					int temp_x = real_x + j * SQUARE_SIZE;
					int temp_y = real_y + i * SQUARE_SIZE;
					int botton_line_y = temp_y + SQUARE_SIZE;

					if (temp_x +SQUARE_SIZE> right_wall)
					{
						return 1;
					}

					int index_x = (temp_y - begin_y) / SQUARE_SIZE;
					int index_y = (temp_x - begin_x) / SQUARE_SIZE;

					if (index_x >= 0 && index_x < 15 && index_y >= 0 && index_y < 10)
					{
						if (out_has_square[index_x][index_y])
						{
							return 1;
						}
					}
				}
			}
		}
		return 0;
	}

	bool CheckCollisionBotton(int begin_x,int begin_y,int botton_wall, bool out_has_square[15][10])
	{
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (has_square[i][j])
				{
					float temp_x = x + j * SQUARE_SIZE;
					float temp_y = y + i * SQUARE_SIZE;
					float botton_line_y = temp_y +SQUARE_SIZE;

					if (botton_line_y >= botton_wall)
					{
						y = (int)((y - begin_y) / SQUARE_SIZE) * SQUARE_SIZE + begin_y;
						return 1;
					}


					int index_x = (int)((botton_line_y - begin_y) / SQUARE_SIZE);
					int index_y = (int)((temp_x - begin_x) / SQUARE_SIZE);

					if (index_x>=0&&index_y>=0&&out_has_square[index_x][index_y])
					{
						y = (int)((y - begin_y) / SQUARE_SIZE) * SQUARE_SIZE + begin_y;
						return 1;
					}
				}
			}
		}

		return 0;
	}

	bool CheckCollisionBotton2(int begin_x, int begin_y, int botton_wall, bool out_has_square[15][10])
	{
		int real_x = (int)((x - begin_x) / SQUARE_SIZE) * SQUARE_SIZE + begin_x;
		int real_y = (int)((y - begin_y) / SQUARE_SIZE) * SQUARE_SIZE + begin_y;

		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (has_square[i][j])
				{
					int temp_x = real_x + j * SQUARE_SIZE;
					int temp_y = real_y + i * SQUARE_SIZE;
					int botton_line_y = temp_y + SQUARE_SIZE;

					if (botton_line_y > botton_wall)
					{
						return 1;
					}

					int index_x = (temp_y - begin_y) / SQUARE_SIZE;
					int index_y = (temp_x - begin_x) / SQUARE_SIZE;

					if (index_x >= 0 && index_x < 15 && index_y >= 0 && index_y < 10)
					{
						if (out_has_square[index_x][index_y])
						{
							return 1;
						}
					}
				}
			}
		}
		return 0;
	}


	bool FixSquare(int begin_x, int begin_y, bool out_has_square[15][10])
	{
		int begin_index_x = (int)((y - begin_y) / SQUARE_SIZE);
		int begin_index_y = (int)((x - begin_x) / SQUARE_SIZE);
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				if (has_square[i][j])
				{
					if (begin_index_x + i >= 0)
					{
						out_has_square[begin_index_x + i][begin_index_y + j] = 1;
					}
					else
					{
						return 1;
					}

				}
			}
		}
		return 0;
	}

	void Rotate()
	{
		int temp[4][4];
		int save[4][4];
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				temp[i][j] = has_square[3-j][i];
			}
		}

		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				has_square[i][j] = temp[i][j];
			}
		}

	}

	void GetSqure(int temp[4][4])
	{
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				temp[i][j] = has_square[i][j];
			}
		}
	}

	void SetSquare(int temp[4][4])
	{
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				has_square[i][j] = temp[i][j];
			}
		}
	}

protected:
	//float dx;
	//float dy;

    float x;
	float y;

	int has_square[4][4];

};

class Square1 :public Square
{
public:
	Square1(int x, int y):Square(x,y)
	{
		has_square[0][2] = 1;
		has_square[1][2] = 1;
		has_square[2][2] = 1;
		has_square[3][2] = 1;
		
	}
};

class Square2 :public Square
{
public:
	Square2(int x, int y) :Square(x, y)
	{
		has_square[1][1] = 1;
		has_square[1][2] = 1;
		has_square[2][1] = 1;
		has_square[2][2] = 1;

	}
};

class Square3 :public Square
{
public:
	Square3(int x, int y) :Square(x, y)
	{
		has_square[0][1] = 1;
		has_square[1][0] = 1;
		has_square[1][1] = 1;
		has_square[1][2] = 1;

	}
};

class Square4 :public Square
{
public:
	Square4(int x, int y) :Square(x, y)
	{
		has_square[0][2] = 1;
		has_square[1][0] = 1;
		has_square[1][1] = 1;
		has_square[1][2] = 1;

	}
};

class Square5 :public Square
{
public:
	Square5(int x, int y) :Square(x, y)
	{
		has_square[0][0] = 1;
		has_square[1][0] = 1;
		has_square[1][1] = 1;
		has_square[1][2] = 1;

	}
};

class Square6 :public Square
{
public:
	Square6(int x, int y) :Square(x, y)
	{
		has_square[1][1] = 1;
		has_square[1][2] = 1;
		has_square[2][0] = 1;
		has_square[2][1] = 1;

	}
};

class Square7 :public Square
{
public:
	Square7(int x, int y) :Square(x, y)
	{
		has_square[1][0] = 1;
		has_square[1][1] = 1;
		has_square[2][1] = 1;
		has_square[2][2] = 1;

	}
};
